package com.shareach.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.common.BizCondition;
import com.shareach.dao.mongo.BaseTelephoneDao;
import com.shareach.domain.BaseTelephoneDomain;
import com.shareach.domain.common.Bind;
import com.shareach.service.BaseTelphoneService;
import com.shareach.service.exception.BaseService;

@Service
public class BaseTelphoneServiceImpl extends BaseService<BaseTelephoneDomain> implements BaseTelphoneService {
	@Autowired
	private BaseTelephoneDao baseTelephoneDao;

	@Override
	public Long save(BaseTelephoneDomain domain) {
		Integer shopCount = (domain.getBind() != null && domain.getBind().size() > 0) ? domain.getBind().size() : 0;
		domain.setShopCount(shopCount);
		Long retId = baseTelephoneDao.insert(domain);
		return retId;
	}

	@Override
	public Long saveOrUpdate(BaseTelephoneDomain domain) {
		Long retId = null;
		if (domain.getNumber() == null || domain.getCity() == null) {
			return retId;
		} else {

			List<BaseTelephoneDomain> baseList = getList(new BizCondition().put("number", domain.getNumber()).put(
					"city", domain.getCity()));

			BaseTelephoneDomain telphone = null;
			if (baseList.size() > 0) {
				telphone = baseList.get(0);
				Set<Bind> bindSet = telphone.getBind();
				Set<Bind> newbindSet = domain.getBind();
				if (bindSet == null) {
					bindSet = new HashSet<Bind>();
				}
				bindSet.addAll(newbindSet);
				baseTelephoneDao.update(Criteria.where("number").is(domain.getNumber()),
						new Update().set("bind", bindSet.toArray()).set("shopCount", bindSet.size()));
				retId = telphone.getId();
			} else {
				retId = save(domain);

			}
			return retId;
		}
	}

	@Override
	public void del(BaseTelephoneDomain domain) {

	}

	@Override
	public void update(BaseTelephoneDomain domain) {

	}

	@Override
	public BaseTelephoneDomain getByTel(String city, String number) {
		Query query = parseConditionToQuery(new BizCondition().put("city", city).put("number", number));
//		Query query = Query.query(critera) QueryBuilder.start().and("city").is(city).and("number").is(number).get();
		return baseTelephoneDao.findOne(query);
	}

	@Override
	public List<BaseTelephoneDomain> getList(BaseTelephoneDomain domain) {
		Query query = parseDomaintoQuery(domain);
		
		List<BaseTelephoneDomain> basebankCardList = baseTelephoneDao.findList(query);
		return basebankCardList;
	}

	@Override
	public void updateByCriteria(Criteria criteria, Update update) {
		baseTelephoneDao.update(criteria, update);
	}

	@Override
	public List<BaseTelephoneDomain> getByCriteria(Criteria criteria) {
		return baseTelephoneDao.findByQuery(criteria);
	}

	@Override
	public void update(BaseTelephoneDomain domain, BizCondition contition) {

	}

	@Override
	public List<BaseTelephoneDomain> getList(BizCondition condition) {
		Query query = parseConditionToQuery(condition);
		List<BaseTelephoneDomain> domainList = baseTelephoneDao.findList(query);
		return domainList;
	}

	@Override
	public Page<BaseTelephoneDomain> getPageResult(BaseTelephoneDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BaseTelephoneDomain> domainPage = baseTelephoneDao.getPageResult(query, order);
		return domainPage;
	}

}
